/**
 * 
 */
package com.dalonedrow.module.hq.systems;

import com.dalonedrow.engine.systems.base.Diceroller;
import com.dalonedrow.utils.UtilityMethods;

/**
 * Class to sort and find treasure in each dungeon.
 * @author Donald
 */
public final class HQTreasureFinder {
	/** the gem treasure card. */
	public static final int			HQ_TREASURE_00_GEM					= 0;
	/** the gold 1 treasure card. */
	public static final int			HQ_TREASURE_01_GOLD_1				= 1;
	/** the gold 2 treasure card. */
	public static final int			HQ_TREASURE_02_GOLD_2				= 2;
	/** the hazard 1 treasure card. */
	public static final int			HQ_TREASURE_03_HAZARD_1				= 3;
	/** the hazard 2 treasure card. */
	public static final int			HQ_TREASURE_04_HAZARD_2				= 4;
	/** the heroic brew treasure card. */
	public static final int			HQ_TREASURE_05_HEROIC_BREW			= 5;
	/** the jewels treasure card. */
	public static final int			HQ_TREASURE_06_JEWELS				= 6;
	/** the potion of defense treasure card. */
	public static final int			HQ_TREASURE_07_POTION_OF_DEFENSE	= 7;
	/** the potion of healing treasure card. */
	public static final int			HQ_TREASURE_08_POTION_OF_HEALING	= 8;
	/** the potion of strength treasure card. */
	public static final int			HQ_TREASURE_09_POTION_OF_STRENGTH	= 9;
	/** the wandering monster treasure card. */
	public static final int			HQ_TREASURE_10_WANDERING_MONSTER	= 10;
	/** flag indicating the slot is empty. */
	public static final int			HQ_TREASURE_NONE					= -1;
	/** the one and only instance of the <code>HQTreasureFinder</code> class. */
	private static HQTreasureFinder	instance;
	/**
	 * Gives access to the singleton instance of {@link HQTreasureFinder}.
	 * @return {@link HQTreasureFinder}
	 */
	public static HQTreasureFinder getInstance() {
		if (HQTreasureFinder.instance == null) {
			HQTreasureFinder.instance = new HQTreasureFinder();
		}
		return HQTreasureFinder.instance;
	}
	/** the list of cards. */
	private int[]	cards;
	/**
	 * Picks a random treasure card from the deck.
	 * @return <code>int</code>
	 */
	public int pickACard() {
		int[] deck = new int[0];
		for (int i = 0; i < cards.length; i++) {
			if (cards[i] != HQ_TREASURE_NONE) {
				UtilityMethods.getInstance().extendIntArray(deck, cards[i]);
			}
		}
		int card = Diceroller.getInstance().getRandomIndex(deck);
		if (card == HQ_TREASURE_00_GEM
				|| card == HQ_TREASURE_01_GOLD_1
				|| card == HQ_TREASURE_02_GOLD_2
				|| card == HQ_TREASURE_05_HEROIC_BREW
				|| card == HQ_TREASURE_06_JEWELS
				|| card == HQ_TREASURE_07_POTION_OF_DEFENSE
				|| card == HQ_TREASURE_08_POTION_OF_HEALING
				|| card == HQ_TREASURE_09_POTION_OF_STRENGTH) {
			// find index matching the card type just picked
			for (int i = 0; i < cards.length; i++) {
				if (card == cards[i]) {
					// set index to empty
					cards[i] = HQ_TREASURE_NONE;
				}
			}
		}
		deck = null;
		return card;
	}
	/** Initializes a new deck of cards. Done at the beginning of a dungeon. */
	public void newDeck() {
		final int num = 24;
		if (cards == null) {
			cards = new int[num];
		}
		int i = 0;
		// 2 GEM cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_00_GEM;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_00_GEM;
		// 2 GOLD 1 cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_01_GOLD_1;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_01_GOLD_1;
		// 2 GOLD 2 cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_02_GOLD_2;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_02_GOLD_2;
		// 2 HAZARD 1 cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_03_HAZARD_1;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_03_HAZARD_1;
		// 2 HAZARD 2 cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_04_HAZARD_2;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_04_HAZARD_2;
		// 1 HEROIC BREW card
		cards[i++] = HQTreasureFinder.HQ_TREASURE_05_HEROIC_BREW;
		// 2 JEWELS cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_06_JEWELS;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_06_JEWELS;
		// 1 POTION OF DEFENSE card
		cards[i++] = HQTreasureFinder.HQ_TREASURE_07_POTION_OF_DEFENSE;
		// 3 POTION OF HEALING cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_08_POTION_OF_HEALING;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_08_POTION_OF_HEALING;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_08_POTION_OF_HEALING;
		// 1 POTION OF STRENGTH card
		cards[i++] = HQTreasureFinder.HQ_TREASURE_09_POTION_OF_STRENGTH;
		// 6 WANDERING MONSTER cards
		cards[i++] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
		cards[i++] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
		cards[i] = HQTreasureFinder.HQ_TREASURE_10_WANDERING_MONSTER;
	}
}
